package com.lxw.rmg.menu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxw.common.utils.PageUtils;
import com.lxw.common.utils.R;
import com.lxw.rmg.menu.entity.BuyEntity;
import com.lxw.rmg.menu.entity.MatterEntity;
import com.lxw.rmg.menu.entity.MenuEntity;
import com.lxw.rmg.menu.entity.SubscribeEntity;
import com.lxw.rmg.menu.service.MatterService;
import com.lxw.rmg.menu.service.MenuService;
import com.lxw.rmg.menu.service.SubscribeService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 物料库存表
 *
 * @author lxw
 * @email
 * @date 2023-03-20 20:02:08
 */
@RestController
@RequestMapping("rmg/buy")
public class BuyController {
    @Autowired
    private MatterService matterService;
    @Autowired
    private SubscribeService subscribeService;
    @Autowired
    private MenuService menuService;

    /**
     * 列表 返回今日需要采购的物料
     */
    @RequestMapping("/list")
//    @RequiresPermissions("rmg:matter:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){

        //获取所有订单
        PageUtils page = subscribeService.queryPage(params);
        //创建采购对象
        ArrayList<BuyEntity> buyEntities = new ArrayList<>();
        HashMap<String, Integer> buyMap = new HashMap<>();
        //首先获取今日订单
        //获取当前时间
        String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());
        List<SubscribeEntity> subscribeEntityList = (List<SubscribeEntity>) subscribeService.list();

        //临时集合
        ArrayList<SubscribeEntity> subscribeEntities = new ArrayList<>();
        for (SubscribeEntity subscribeEntity : subscribeEntityList) {
            //获取预约时间
            String orderTime = subscribeEntity.getOrderTime();
            //截取
            String[] sDate = orderTime.split(" ");
            if (nowDate.equals(sDate[0])){
                //如果是同一天就进入 并获取消耗物料
                //获取菜名
                String dishName = subscribeEntity.getDishName();
                //通过菜名到菜单中查询
                MenuEntity menu = menuService.getOne(new QueryWrapper<MenuEntity>().eq("dish_name", dishName));
                if (null == menu ){
                    continue;
                }
                //获取当前订单中所需要的物料
                String reqMate = menu.getReqMate();
                //三元运算
               String[] req =  reqMate.contains(",") ? reqMate.split(",") : reqMate.split("_") ;


                for (int i = 0; i < req.length; i++) {

                    String[] r =  req[i].contains("_") ? req[i].split("_") : req ;
                    for (int j = 0; j < r.length; j++) {

                        //如果等于null或0就是没有
                        if (null ==  buyMap.get(r[0]) || 0 == buyMap.get(r[0])){
                            buyMap.put(r[0],Integer.parseInt(r[1]));
                        }else {
                            buyMap.put(r[0],buyMap.get(r[0])+Integer.parseInt(r[1]));
                        }

                    }
                }


            }
        }

        buyMap.forEach((key,value)->{
            BuyEntity buyEntity = new BuyEntity();
            buyEntity.setMateName(key);
            buyEntity.setTodayConsume(value);
            buyEntities.add(buyEntity);
        });
        page.setList(buyEntities);
        System.out.println("==============================================");
        System.out.println(buyEntities);
        System.out.println("==============================================");
        return R.ok().put("page", page);
    }

//
//    /**
//     * 信息
//     */
//    @RequestMapping("/info/{id}")
////    @RequiresPermissions("rmg:matter:info")
//    public R info(@PathVariable("id") Integer id){
//		MatterEntity matter = matterService.getById(id);
//
//        return R.ok().put("matter", matter);
//    }
//
//    /**
//     * 保存
//     */
//    @RequestMapping("/save")
////    @RequiresPermissions("rmg:matter:save")
//    public R save(@RequestBody MatterEntity matter){
//		matterService.save(matter);
//
//        return R.ok();
//    }
//
//    /**
//     * 修改
//     */
//    @RequestMapping("/update")
////    @RequiresPermissions("rmg:matter:update")
//    public R update(@RequestBody MatterEntity matter){
//		matterService.updateById(matter);
//
//        return R.ok();
//    }
//
//    /**
//     * 删除
//     */
//    @RequestMapping("/delete")
////    @RequiresPermissions("rmg:matter:delete")
//    public R delete(@RequestBody Integer[] ids){
//		matterService.removeByIds(Arrays.asList(ids));
//
//        return R.ok();
//    }

}
